package com.cimr.comm.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.cimr.comm.dao.GroupDao;
import com.cimr.comm.po.Group;
import com.cimr.comm.service.GroupService;
import com.cimr.comm.service.RoleService;
import com.cimr.comm.service.UserService;
import com.cimr.comm.token.TokenUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Created by liqi on 2018/1/31.
 * liqiwork@qq.com
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {
    @Override
    public List<Group> getGroupAndSonsByGroupId(String groupId) {
        return groupDao.getGroupAndSonsByGroupId(groupId);
    }

    @Override
    public List<Group> getSonsByGroupId(String groupId) {
        return groupDao.getSonsByGroupId(groupId);
    }

    @Override
    public List<Group> getGroupListWithName(String groupName, String groupId) {
        if (TokenUtil.isAdministrator()) {
            return groupDao.getGroupListWithName(groupName, null);
        } else if (groupId!=null && !Objects.equals("", groupId)) {
            return groupDao.getGroupListWithName(groupName, groupId);
        }
        return null;
    }

    @Override
    public List<Group> getGroupListByName(String groupName, String parentId) {
        return groupDao.getGroupListByName(groupName, parentId);
    }

    @Override
    public Group getGroupById(String id) {
        return groupDao.getGroupById(id);
    }

    @Override
    public int updateOrderId(String id, Float orderId) {
        return groupDao.updateObject(id, null, null, null, orderId, null, null, null);
    }

    @Override
    public int insertGroup(String id, String groupName, String comment, Float orderId, String parentId, String parentName) {
        if (parentId!=null && !Objects.equals("", parentId) && !Objects.equals("0", parentId)) {
            Group parentGroup = getGroupById(parentId);
            if (parentGroup!=null) {
                String parentIdsStr = parentGroup.getParentIds();
                JSONArray parentIdsArray = JSONUtil.createArray();
                if (parentIdsStr != null && !Objects.equals("", parentIdsStr)) {
                    parentIdsArray = JSONUtil.parseArray(parentGroup.getParentIds());
                }
                parentIdsArray.add(parentId);
                parentIdsStr = JSONUtil.toJsonStr(parentIdsArray);

                Integer parentGrade = parentGroup.getGrade();
                Integer grade = 1;
                if (grade!=null) {
                    grade = parentGrade+1;
                }

                return groupDao.insertObject(id, null, groupName, comment, orderId, parentId, parentName, parentIdsStr, grade);
            }
        }
        return 0;
    }

    @Override
    public int updateGroup(String id, String groupName, String comment, Float orderId, String parentId, String parentName) {
//        if (parentId!=null && !Objects.equals("",parentId)) {
            // 不允许修改上级组织
            return groupDao.updateObject(id, null, groupName, comment, orderId, null, null, null);
//        }
//        else {
//            User currentUser = TokenUtil.getToken();
//            if (currentUser != null && currentUser.getGroupId() != null && !Objects.equals("", currentUser.getGroupId())) {
//                return groupDao.updateObject(id, null, groupName, comment, orderId, currentUser.getGroupId(), currentUser.getGroupName());
//            }
//        }
//        return 0;
    }

    @Override
    public int deleteGroupById(String id) {
        // 判断是否有下级组织、用户、角色
        List sons = getSonsByGroupId(id);
        if (sons!=null && sons.size()>0) {
            return 0;
        }
        List users = userService.getUserListByGroupId(id);
        if (users!=null && users.size()>0) {
            return 0;
        }
        List roles = roleService.getRoleListByGroupId(id);
        if (roles!=null && roles.size()>0){
            return 0;
        }
        return groupDao.deleteObjectById(id);
    }

    @Override
    public List<Group> getGroupList() {
        return groupDao.getGroupList();
    }


    @Autowired
    private GroupDao groupDao;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
}
